package com.gs.montaditos;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ListOrder extends Activity {
	// Esto irá en la clase ListMontaditos cuando sepa inicializarla sin tener q
	// cargarla previamente
	public static HashMap<Integer, String> montaditos = new HashMap<Integer, String>();
	SimpleAdapter adapter;
	Boolean waiter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Intent i = getIntent();
		waiter = i.getBooleanExtra("waiter", false);
	}
	/** Called when the activity is first created. */
	@Override
	protected void onStart() {
		super.onStart();
		if(!waiter){
			setContentView(R.layout.order);
			Button random = (Button) findViewById(R.id.random);
			Button back = (Button) findViewById(R.id.back);
			random.setOnClickListener(new OnClickListener() {        	
				public void onClick(View v) {
					startActivity(new Intent(getApplicationContext(), RandomOrder.class));
				}
			});
			back.setOnClickListener(new OnClickListener() {        	
				public void onClick(View v) {
					startActivity(new Intent(getApplicationContext(), mainActivity.class));
				}
			});
		} else{
			setContentView(R.layout.order_waiter);
		}
		
		
		ListView lv = (ListView) findViewById(R.id.listOrder);
		setAdapter();
	}
	List<HashMap<String, String>> fillMaps;
	private void setAdapter() {
		ListView lv = (ListView) findViewById(R.id.listOrder);
		// create the grid item mapping
		String[] from = new String[] { "orderId", "montaditoId", "name",
				"amount", "amount_string", "image" };
		int[] to = new int[] { R.id.orderId, R.id.montaditoId, R.id.name,
				R.id.amount, R.id.amount_string, R.id.image };
		
        HashMap<Integer, Integer> items = Order.getAll();
        // prepare the list of all records
        fillMaps = new ArrayList<HashMap<String, String>>();
        for(Integer id : items.keySet()){
                HashMap<String, String> insert = Montaditos.get(id);
                insert.put("amount", String.valueOf(items.get(id)));
                insert.put("amount_string", "Cantidad: " + String.valueOf(items.get(id)));
                fillMaps.add(insert);
        }        
        if (fillMaps.size() == 0) {
        	TextView emptyText = (TextView)findViewById(R.id.empty_order);
        	emptyText.setVisibility(View.VISIBLE);
        }
        if(!waiter){
        	adapter = new SimpleAdapter(this, fillMaps,R.layout.orderitem, from, to);
        	setEvents(lv);
        } else{
        	adapter = new SimpleAdapter(this, fillMaps,R.layout.order_item_waiter, from, to);
        }
		
		lv.setAdapter(adapter);
	}
	
	private void setEvents(ListView lv){
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long id) {
				final Integer pos = position;
				final View v = view;
				final Integer selected = new Integer(((TextView) view
						.findViewById(R.id.montaditoId)).getText().toString());
				AlertDialog.Builder dialog = new AlertDialog.Builder(
						ListOrder.this);
				dialog.setMessage(getString(R.string.list_text));
				dialog.setTitle("");
				dialog.setNegativeButton(R.string.remove,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								Order.remove(selected);
								HashMap tmp = (HashMap)ListOrder.this.adapter.getItem(pos);
								Integer a = new Integer(new Integer((String)tmp.get("amount").toString()) - 1);
								if (a > 0) {
									tmp.put("amount", a);
					                tmp.put("amount_string", "Cantidad: " + a);
								} else {
									Iterator it = fillMaps.iterator();
									while (it.hasNext()) {
										HashMap m = (HashMap) it.next();
										TextView id = (TextView)v.findViewById(R.id.montaditoId);
										
										if (m.get("montaditoId").equals(id.getText())) {
											fillMaps.remove(m);
											break;
										}
									}
								}
								ListOrder.this.adapter.notifyDataSetChanged(); 
								ListOrder.this.adapter.notifyDataSetInvalidated();
							}
						});
				dialog.setPositiveButton(R.string.add,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								Order.add(selected);
								HashMap tmp = (HashMap)ListOrder.this.adapter.getItem(pos);
								Integer a = new Integer(new Integer(tmp.get("amount").toString()) + 1);
								tmp.put("amount", a);
				                tmp.put("amount_string", "Cantidad: " + a);
								ListOrder.this.adapter.notifyDataSetChanged(); 
								ListOrder.this.adapter.notifyDataSetInvalidated();
							}
						});
				dialog.setNeutralButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				dialog.show();
			}
		});
	}
	
}
